package com.huobao.yunying.fragments;

import java.util.List;
import java.util.Map;

import org.xutils.common.util.DensityUtil;
import org.xutils.image.ImageOptions;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.huobao.yunying.adapters.Adapter_Ketang_more;
import com.huobao.yunying.entity.Fragment_ketang_jiangshi;
import com.huobao.yunying.entity.Fragment_ketang_jieshao;
import com.huobao.yunying.entity.KetangMore;
import com.huobao.yunying.https.Http;
import com.huobao.yunying.https.HttpConst;
import com.huobao.yunying.https.HttpListener;
import com.huobao.yunying.https.HttpUtil;
import com.huobao.yunying.utils.ImageUtils;
import com.huobao.yunying.views.NoScrollListView;
import com.huobao.yunying.views.RoundImageView;
import com.huobao.zhangying.R;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageView;
import android.widget.TextView;

public class Fragment_ketang_detail extends BaseFragment implements OnItemClickListener{
	/** 课程名称 */
	private TextView tv_kecheng_name;
	/** 课程简洁 */
	private TextView tv_kecheng_content;
	/** 头像 */
	private RoundImageView imageview;
	/** 姓名 */
	private TextView tv_name;
	/** 介绍 */
	private TextView tv_jieshao;
	private NoScrollListView listview;
	private Adapter_Ketang_more adapter;


	public static Fragment_ketang_detail getInstant() {
		return new Fragment_ketang_detail();
	}

	private String id;


	public void setid(String id) {
		this.id = id;
		getDetail(id);
	}

	@Override
	public View onCreateContentView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.layout_fragment_ketangdetail, null);
		findviews(view);
		imageview.setRectAdius(90);
		adapter = new Adapter_Ketang_more(getActivity());
		listview.setOnItemClickListener(this);
		listview.setAdapter(adapter);
		return view;
	}

	private void findviews(View view) {
		tv_kecheng_name = (TextView) view.findViewById(R.id.tv_title);
		tv_kecheng_content = (TextView) view.findViewById(R.id.tv_content);
		tv_jieshao = (TextView) view.findViewById(R.id.tv_jieshao);
		tv_name = (TextView) view.findViewById(R.id.tv_name);
		imageview = (RoundImageView) view.findViewById(R.id.imageview);
		listview = (NoScrollListView) view.findViewById(R.id.listview);
	}

	private void getDetail(String strid) {
		Map<String, String> map;
		if(strid == null){
			map = HttpUtil.getParams(HttpConst.FRIST_KEY, "ketang", HttpConst.SECOND_KEY, "kchshow",
					"id", id);
		}else{
			map = HttpUtil.getParams(HttpConst.FRIST_KEY, "ketang", HttpConst.SECOND_KEY, "kchshow",
					"id", strid);
		}
		
		Http.getInstant().sendRequest(getActivity(), false, map, new HttpListener() {

			@Override
			public void onHttpSuccess(String result) {
				JSONObject jsonObject = JSON.parseObject(result);
				jsonObject = jsonObject.getJSONObject("retval");
				Fragment_ketang_jieshao jieshao = JSON.parseObject(jsonObject.getString("skecheng"),
						Fragment_ketang_jieshao.class);
				Fragment_ketang_jiangshi jiangshi = JSON
						.parseArray(jsonObject.getString("slecturer"), Fragment_ketang_jiangshi.class).get(0);
				List<KetangMore> list = JSON.parseArray(jsonObject.getString("srelevant"), KetangMore.class);
				adapter.clear();
				adapter.addAll(list);
				showJieshao(jieshao);
				showJiangshi(jiangshi);
				showContentView();
			}

			private void showJiangshi(Fragment_ketang_jiangshi jiangshi) {
				ImageOptions imageOptions = new ImageOptions.Builder()
						.setSize(DensityUtil.dip2px(320), DensityUtil.dip2px(320))// 图片大小
						// .setRadius(DensityUtil.dip2px(5))//ImageView圆角半径
						// .setCrop(true)// 如果ImageView的大小不是定义为wrap_content,
						// 不要crop.
						.setImageScaleType(ImageView.ScaleType.CENTER_CROP)
						.setLoadingDrawableId(R.mipmap.imageloading)// 加载中默认显示图片
						// .setFailureDrawableId(R.mipmap.ic_launcher)//加载失败后默认显示图片
						.build();
				ImageUtils.loadimg(imageview, imageOptions, jiangshi.getLecturer_img(), null);
				tv_name.setText(jiangshi.getLecturer_name());
				tv_jieshao.setText(jiangshi.getLecturer_intro());
			}

			private void showJieshao(Fragment_ketang_jieshao jieshao) {
				tv_kecheng_name.setText(jieshao.getCourse_title());
				tv_kecheng_content.setText(jieshao.getCourse_intro());
			}

			@Override
			public void onHttpError(String error) {
			}
		});
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
		getDetail(adapter.getList().get(position).getCourse_id());
	}
}
